package com.smartleanx.module.hrm.controller.admin.examine.examinSon;

import com.alibaba.fastjson2.JSONObject;
import com.smartleanx.module.hrm.controller.admin.examine.examinSon.vo.ExaminePageReqVO;
import com.smartleanx.module.hrm.controller.admin.examine.examinSon.vo.ExamineRespVO;
import com.smartleanx.module.hrm.controller.admin.examine.examinSon.vo.ExamineSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examinSon.ExamineDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRecord.ExamineRecordDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRecordRater.ExamineRecordRaterDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.vo.ExamineEmployeeFileExcel;
import com.smartleanx.module.hrm.dal.dataobject.examine.vo.ExamineRecordExcelVo;
import com.smartleanx.module.hrm.service.examine.examinSon.ExamineService;
import com.smartleanx.module.infra.api.file.dto.FileRefRespDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.framework.excel.core.util.ExcelUtils;

import com.smartleanx.framework.operatelog.core.annotations.OperateLog;

import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 考核计划")
@RestController
@RequestMapping("/hrm/examine")
@Validated
public class ExamineController {

    @Resource
    private ExamineService examineService;

    @PostMapping("/create")
    @Operation(summary = "创建考核计划")
//    @PreAuthorize("@ss.hasPermission('hrm:examine:create')")
    public CommonResult<Integer> createExamine(@RequestBody JSONObject param) {
        return success(examineService.createExamine(param));
    }

    @PostMapping("/update")
    @Operation(summary = "更新考核计划")
    @PreAuthorize("@ss.hasPermission('hrm:examine:update')")
    public CommonResult<Integer> updateExamine(@Valid @RequestBody JSONObject param) {
        Integer i = examineService.updateExamine(param);
        return success(i);
    }

    @GetMapping("/deletedExamineRecordAll")
    @Operation(summary = "删除考核人员")
    public CommonResult<Object> deletedExamineRecordAll(@RequestParam("employeeId") Integer id, @RequestParam("examineId") Integer examineId) {
        examineService.deletedExamineRecordAll(id, examineId);
        return success("");
    }

    @GetMapping("/delete")
    @Operation(summary = "删除考核计划")

    public CommonResult<Boolean> deleteExamine(@RequestParam("id") Integer id) {
        examineService.deleteExamine(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考核计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine:query')")
    public CommonResult<ExamineRespVO> getExamine(@RequestParam("id") Integer id) {
        ExamineDO examine = examineService.getExamine(id);
        return success(BeanUtils.toBean(examine, ExamineRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考核计划分页")
    @PreAuthorize("@ss.hasPermission('hrm:examine:query')")
    public CommonResult<PageResult<ExamineRespVO>> getExaminePage(@Valid ExaminePageReqVO pageReqVO) {
        PageResult<ExamineDO> pageResult = examineService.getExaminePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamineRespVO.class));
    }

    @GetMapping("/exportTemplatExcel")
    @PreAuthorize("@ss.hasPermission('hrm:examine:export')")
    public void exportExamineExcel(HttpServletResponse response) throws IOException {
        List<ExamineRecordDO> list = examineService.selectExamineFileExcel();
        // 导出 Excel
        ExcelUtils.write(response, "考核档案.xlsx", "数据", ExamineRecordExcelVo.class, BeanUtils.toBean(list, ExamineRecordExcelVo.class));
    }

    @GetMapping("/exportExamineInfoExcel")
    @PreAuthorize("@ss.hasPermission('hrm:examine:infoExport')")
    public void exportExamineInfoExcel(HttpServletResponse response,@RequestParam("employeeId")Integer employeeId,@RequestParam(value = "examinePlanName",defaultValue = "")String examinePlanName ) throws IOException {
        List<ExamineRecordDO> list = examineService.selectExamineFileInfoExcelList(employeeId,examinePlanName);
        // 导出 Excel
        ExcelUtils.write(response, "考核档案员工.xlsx", "数据", ExamineEmployeeFileExcel.class, BeanUtils.toBean(list, ExamineEmployeeFileExcel.class));
    }


    @GetMapping("/exportEmployeeFileExcel")
    @PreAuthorize("@ss.hasPermission('hrm:examine:infoExport')")
    public void exportEmployeeFileExcel(HttpServletResponse response) throws IOException {
        List<ExamineRecordDO> list = examineService.selectEmployeeExamineExcelFileList();
        // 导出 Excel
        ExcelUtils.write(response, "考核档案员工.xlsx", "数据", ExamineEmployeeFileExcel.class, BeanUtils.toBean(list, ExamineEmployeeFileExcel.class));
    }

    @GetMapping("/getById")
    public CommonResult<Object> getById(Integer id) {
        final JSONObject jsonObject = examineService.selectExamineVoList(id);
        return success(jsonObject);
    }


    @GetMapping("/startExamine")
    public CommonResult<Object> startExamine(Integer id) {
        final Integer integer = examineService.startExamine(id);
        return CommonResult.success(integer);
    }


    @GetMapping("/getExamineEmployeeList")
    public CommonResult<Object> getExamineEmployeeList(@RequestParam("id") Integer id) {
        final JSONObject result = examineService.selectExamineEmployeeList(id);
        return CommonResult.success(result);
    }

    @GetMapping("/getExamine")
    public CommonResult<Object> getExamineById(@RequestParam("id") Integer id) {
        final ExamineDO result = examineService.selectExamine(id);
        return CommonResult.success(result);
    }

    @GetMapping("/getExamineStartedList")
    public CommonResult<Object> getExamineStartedList(@RequestParam("type") Integer type, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam("examinePlanName") String examinePlanName,@RequestParam(value = "examineCycleTypeId",defaultValue = "")Integer examineCycleTypeId) {
        final PageResult<ExamineDO> examineStartedList = examineService.getExamineStartedList(type, pageNum, pageSize, examinePlanName,examineCycleTypeId);
        return CommonResult.success(examineStartedList);
    }

    @GetMapping("/startProgress")
    public CommonResult<Object> getExamineStartedList(@RequestParam("id") Integer examineId) {
        final Integer integer = examineService.startProgress(examineId);
        return CommonResult.success(integer);
    }

    @GetMapping("/selectExamineByEmployeeId")
    public CommonResult<Object> selectExamineBYEmployeeId(@RequestParam("type") Integer type, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        final PageResult<ExamineRecordRaterDO> examineStartedList = examineService.selectExamineBYEmployeeId(type, pageNum, pageSize);
        return CommonResult.success(examineStartedList);
    }

    @GetMapping("/selectExamineBYReviewEmployeeId")
    public CommonResult<Object> selectExamineBYReviewEmployeeId(@RequestParam("type") Integer type, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        final PageResult<ExamineRecordDO> examineStartedList = examineService.selectExamineBYReviewEmployeeId(type, pageNum, pageSize);
        return CommonResult.success(examineStartedList);
    }

    @GetMapping("/progress")
    public CommonResult<JSONObject> progress(@RequestParam("id") Integer id, @RequestParam("scorerOrder") Integer scorerOrder) {
        JSONObject progress = examineService.progress(id, scorerOrder);
        return success(progress);
    }

    @PostMapping("/startProgress")
    public CommonResult<Integer> startProgress(@RequestBody JSONObject param) {
        Integer i = examineService.startProgress(param);
        return success(i);
    }


    @PostMapping("/checkProgress")
    public CommonResult<JSONObject> checkProgress(@RequestBody List<JSONObject> param) {
        JSONObject i = examineService.checkProgress(param);
        return success(i);
    }

    @PostMapping("/selectExamineInfoByEmployee")
    public CommonResult<Object> selectExamineInfoByEmployee(@RequestBody JSONObject param) {
        JSONObject jsonObject = examineService.selectExamineInfoByEmployee(param);
        return success(jsonObject);
    }


    @PostMapping("/selectExamineInfoById")
    public CommonResult<Object> selectExamineInfoById(@RequestBody JSONObject param) {
        JSONObject jsonObject = examineService.selectExamineInfoById(param);
        return success(jsonObject);
    }


    @GetMapping("/selectStartReviewList")
    public CommonResult<Object> selectStartReviewList(@RequestParam(value = "id") Integer id, @RequestParam(value = "type") Integer type) {
        List<ExamineRecordDO> examineRecordDOS = examineService.selectStartReviewList(id, type);
        return CommonResult.success(examineRecordDOS);
    }

    @GetMapping("/startReview")
    public CommonResult<Object> startReview(@RequestParam(value = "id") Integer id) {
        Integer i = examineService.startReview(id);
        return CommonResult.success(i);
    }

    @GetMapping("/selectExamineScheduleList")
    public CommonResult<Object> selectExamineScheduleList(@RequestParam(value = "examineId") Integer examineId, @RequestParam(value = "gradeId") Integer gradeId, @RequestParam(value = "employeeName") String employeeName, @RequestParam("type") Integer type) {

        return CommonResult.success(examineService.selectExamineScheduleList(examineId, gradeId, employeeName, type));
    }

    @GetMapping("/selectExamineScheduleEmployeeList")
    public CommonResult<Object> selectExamineScheduleEmployeeList(@RequestParam(value = "examineId") Integer examineId, @RequestParam(value = "status") Integer status, @RequestParam(value = "employeeName") String employeeName) {
        return CommonResult.success(examineService.selectExamineScheduleEmployeeList(examineId, status, employeeName));
    }

    
    /**
     * 员工端审核
     */
    @PostMapping("/review")
    public CommonResult<Object> review(@RequestBody JSONObject param) {
        Integer review = examineService.review(param);

        return CommonResult.success(review);
    }

    /**
     * 员工端确认
     */
    @PostMapping("/confirm")
    public CommonResult<Object> confirm(@RequestBody JSONObject param) {

        Integer confirm = examineService.confirm(param);
        return CommonResult.success(confirm);

    }


    @PostMapping("/adminConfirm")
    public CommonResult<Object> adminConfirm(@RequestBody JSONObject param){
        Integer i = examineService.adminConfirm(param);
        return CommonResult.success(i);
    }
    @PostMapping("/confirmByAdmin")
    public CommonResult<Object> confirmByAdmin(@RequestBody JSONObject param) {

        Integer confirm = examineService.confirmByAdmin(param);
        return CommonResult.success(confirm);

    }

    @GetMapping("/launchResultList")
    public CommonResult<Object> launchResultList(@RequestParam("examineId") Integer examineId) {
        List<ExamineRecordDO> examineRecordDOS = examineService.launchResultList(examineId);
        return CommonResult.success(examineRecordDOS);
    }

    @PostMapping("/launchResult")
    public CommonResult<Object> launchResult(@RequestBody JSONObject param) {
        Integer i = examineService.launchResult(param);
        return CommonResult.success(i);
    }


    @PostMapping("/withdrawResult")
    public CommonResult<Object> withdrawResult(@RequestBody JSONObject param) {
        Integer i = examineService.withdrawResult(param);
        return CommonResult.success(i);
    }


    /**
     * 员工端审核
     */
    @GetMapping("/nodeList")
    public CommonResult<Object> review(@RequestParam(value = "recordId") Integer recordId) {
        List<JSONObject> nodeList = examineService.nodeList(recordId);

        return CommonResult.success(nodeList);
    }


    @GetMapping("/selectLaunchResultExamineInfoByIdWeb")
    public CommonResult<Object> selectLaunchResultExamineInfoByIdWeb(@RequestParam("examineId") Integer examineId) {
        JSONObject jsonObject = examineService.selectLaunchResultExamineInfoByIdWeb(examineId);
        return CommonResult.success(jsonObject);
    }


    @GetMapping("/selectLaunchResultExamineInfoById")
    public CommonResult<Object> selectLaunchResultExamineInfoById(@RequestParam("examineId") Integer examineId) {
        JSONObject jsonObject = examineService.selectLaunchResultExamineInfoById(examineId);
        return CommonResult.success(jsonObject);
    }


    @GetMapping("/launchNodeList")
    public CommonResult<Object> launchNodeList(@RequestParam(value = "recordId") Integer recordId) {
        List<JSONObject> nodeList = examineService.nodeList(recordId);

        return CommonResult.success(nodeList);
    }


    /**
     * app端列表
     */
    @GetMapping("/selectLaunchResultList")
    private CommonResult<Object> selectLaunchResultList() {
        List<JSONObject> jsonObjects = examineService.selectLaunchResultList();
        return CommonResult.success(jsonObjects);
    }


    /**
     * 归档
     */
    @GetMapping("/examineFile")
    public CommonResult<Object> examineFile(@RequestParam("examineId") Integer examineId) {
        Integer i = examineService.examineFile(examineId);
        return CommonResult.success(i);
    }

    @GetMapping("/selectExamineFileList")
    public CommonResult<Object> selectExamineFileList(@RequestParam("pageNum") Integer pageNUm, @RequestParam("pageSize") Integer pageSize,
                                                      @RequestParam(value = "employeeName",defaultValue = "") String employeeName,
                                                      @RequestParam(value = "employeeNo",defaultValue = "") String employeeNo,
                                                      @RequestParam(value ="deptId",defaultValue = "") Integer deptId,
                                                      @RequestParam(value ="postId",defaultValue = "") Integer postId,
                                                      @RequestParam(value ="employeeState",defaultValue = "")Integer employeeState,
                                                      @RequestParam(value ="sourceUnit",defaultValue = "")Integer sourceUnit,
                                                      @RequestParam(value ="sources",defaultValue = "")Integer sources) {
        PageResult<ExamineRecordDO> examineRecordDOPageResult = examineService.selectExamineFileList(pageNUm, pageSize,employeeName,employeeNo,deptId,postId,employeeState,sourceUnit,sources);
        return success(examineRecordDOPageResult);
    }

    @GetMapping("/selectExamineFileInfoList")
    public CommonResult<Object> selectExamineFileInfoList(@RequestParam("employeeId") Integer employeeId, @RequestParam(value = "examinePlanName", defaultValue = "") String examinePlanName) {
        List<ExamineRecordDO> examineRecordDOPageResult = examineService.selectExamineFileInfoList(employeeId, examinePlanName);
        return success(examineRecordDOPageResult);
    }


    @GetMapping("/selectEmployeeExamineFileList")
    public CommonResult<Object> selectEmployeeExamineFileList(@RequestParam("pageNum") Integer pageNUm, @RequestParam("pageSize") Integer pageSize) {
        PageResult<ExamineRecordDO> examineRecordDOPageResult = examineService.selectEmployeeExamineFileList(pageNUm, pageSize);
        return success(examineRecordDOPageResult);
    }

    @GetMapping("/selectEmployeeExamineFileInfoList")
    public CommonResult<Object> selectEmployeeExamineFileInfoList(@RequestParam("recordId") Integer recordId) {
        ExamineRecordDO examineRecordDO = examineService.selectEmployeeExamineFileInfoList(recordId);
        return success(examineRecordDO);
    }

    @GetMapping("/selectFileTitle")
    public CommonResult<Object> selectFileTitle(@RequestParam("employeeId") Integer employeeId) {

        JSONObject jsonObject = examineService.selectFileTitle(employeeId);
        return success(jsonObject);
    }

    @GetMapping("/selectjdTitle")
    public CommonResult<Object> selectjdTitle(@RequestParam("examineId") Integer examineId) {

        JSONObject jsonObject = examineService.selectjdTitle(examineId);
        return success(jsonObject);
    }

    @GetMapping("/insertIsLowFile")
    public CommonResult<Object> insertIsLowFile(@RequestParam("examineRecordId") Integer examineRecordId, @RequestParam("file") String file) {

        Integer i = examineService.insertIsLowFile(examineRecordId, file);
        return success(i);
    }


    @GetMapping("/selectIsLowFile")
    public CommonResult<Object> selectIsLowFile(@RequestParam("examineRecordId") Integer examineRecordId) {
        List<FileRefRespDTO> f = examineService.selectIsLowFile(examineRecordId);
        return success(f);
    }


    @GetMapping("/selectDealtWithNum")
    public CommonResult<Object> selectDealtWithNum() {
       JSONObject result = examineService.selectDealtWithNum();
        return success(result);
    }

    @GetMapping("/progressMessage")
    public CommonResult<Object> progressMessage(@RequestParam("type")Integer type,@RequestParam("recordId")Integer recordId) {
       Integer status = examineService.progressMessage(type ,recordId);
        return success(status);
    }

}